package com.module.controller;


import com.module.framework.domain.NowLend;
import com.module.framework.service.NowLendService;
import com.module.framework.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ScheduleTask {

    @Autowired
    private NowLendService nowLendService;

    @Autowired
    private UserService userService;

    @Async
    @Scheduled(cron = "0/30 * * * * ? ")
    public void timeTaskTwo() throws ParseException {
        //时间处理
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = simpleFormat.format(new Date());

        //处理时间戳
        long current = simpleFormat.parse(nowDate).getTime();

        List<NowLend> lists = nowLendService.findAllLend();

        for (int i = 0; i < lists.size(); i++) {
            NowLend nowLend = lists.get(i);
            String sqlTieme = simpleFormat.format(nowLend.getDeadline());
            long time = simpleFormat.parse(sqlTieme).getTime();
            if (time < current && nowLend.getDeadFlag() == 0) {
                userService.updateBan(nowLend.getUserId(), nowLend.getId());
            }
        }
    }


}
